package com.dahaiwuliang.code.shiro.realm;

import com.dahaiwuliang.code.pojo.User;
import com.dahaiwuliang.code.server.UserServer;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.Set;

public class MyRealm extends AuthorizingRealm {
      @Autowired
      private UserServer userServer;


    /**
     * 用户授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String userName= (String) SecurityUtils.getSubject().getPrincipal();
           User user =userServer.findByUserName(userName);
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        Set<String> roles=new HashSet<>();
        if ("管理员".equals(user.getRoleName())){
            roles.add("管理员");

            info.addStringPermission("进入管理员主页");
            info.addStringPermission("根据id查询资源类型实体");
            info.addStringPermission("添加或修改资源类型");
            info.addStringPermission("删除资源类型信息" );
            info.addStringPermission("分页查询资源信息列表");
            info.addStringPermission("删除资源");
            info.addStringPermission("修改资源");
            info.addStringPermission("审核资源");
            info.addStringPermission("生成所有资源帖子索引");
            info.addStringPermission("分页查询评论信息");
            info.addStringPermission("删除评论资源");
            info.addStringPermission("修改评论状态");
            info.addStringPermission("分页查询用户信息列表");
            info.addStringPermission("修改是否是Vip");
            info.addStringPermission("是否封禁用户");
            info.addStringPermission("重置密码");
            info.addStringPermission("修改积分");
            info.addStringPermission("修改vip等级");
            info.addStringPermission("分页查询友情链接");
            info.addStringPermission("添加或者修改友情链接");
            info.addStringPermission("根据id查询友情链接");
            info.addStringPermission("删除友情链接");
            info.addStringPermission("修改密码");
            info.addStringPermission("是否热门资源");
            info.addStringPermission("是否免费资源");

        }
        info.setRoles(roles);
        return info;
    }

    /**
     * 用户登入
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
       String userName=(String)authenticationToken.getPrincipal();
        User user=userServer.findByUserName(userName);
        if (user==null){
            return null;
        }else {
            AuthenticationInfo authenticationInfo =new SimpleAuthenticationInfo(user.getUserName(),user.getPassword(),"xx");
            return authenticationInfo;
        }

    }
}
